import logging
import hmac

from pylons import request, response, session, tmpl_context as c
from pylons.controllers.util import abort, redirect

from formalchemy import FieldSet

import papyrus.lib.helpers as h
from papyrus.lib.base import BaseController, render
from papyrus.model import User

login_form = FieldSet(User)
login_form.configure(include=[login_form.email, login_form.password.password()])

log = logging.getLogger(__name__)


class AuthController(BaseController):

    def index(self):
        # Return a rendered template
        #return render('/auth.mako')
        # or, return a response
        return 'Hello World'

    def login(self):
        """Called on login form POST."""
        if not request.method == "POST":
            c.fs = login_form
            return render('/admin/auth/login.mako')

        if "email" not in request.POST and "password" not in request.POST:
            c.fs = login_form
            return render('/admin/auth/login.mako')

        # Get credentials
        email = request.POST["email"]
        password = request.POST["password"]

#        request.environ["repoze.who.userid"]

        user = meta.Session.query(User).filter_by(email=email, password=password)
        return render('/admin/auth/login.mako')

    def login_form(self):
        c.fs = login_form
        return render('/admin/auth/login.mako')
